<?php
// 应用公共文件

if (!function_exists('sys_config')) {
    /**
     * 获取系统单个配置
     * @param string $name
     * @param string $default
     * @return string
     */
    function sys_config(string $name, string $default = ''): string
    {
        if (empty($name))
            return $default;

        $config = trim(app('sysConfig')->get($name));
        if ($config === '') {
            return $default;
        } else {
            return $config;
        }
    }
}

if (!function_exists('sort_list_tier')) {
    /**
     * 分级排序
     * @param $data
     * @param int|string $pid
     * @param string $field
     * @param string $pk
     * @param string $html
     * @param int $level
     * @param bool $clear
     * @return array
     */
    function sort_list_tier($data, $pid = 0, string $field = 'pid', string $pk = 'id', string $html = '|-----', int $level = 1, bool $clear = true): array
    {
        static $list = [];
        if ($clear) $list = [];
        foreach ($data as $k => $res) {
            if ($res[$field] == $pid) {
                $res['html'] = str_repeat($html, $level);
                $list[] = $res;
                unset($data[$k]);
                sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
            }
        }
        return $list;
    }
}

if (!function_exists('get_this_class_methods')) {
    /**
     * 获取当前类方法
     * @param $class
     * @param array $unArray
     * @return array
     */
    function get_this_class_methods($class, array $unArray = []): array
    {
        $arrayAll = get_class_methods($class);
        if ($parent_class = get_parent_class($class)) {
            $arrayParent = get_class_methods($parent_class);
            $arrayNow = array_diff($arrayAll, $arrayParent); // 去除父级的
        } else {
            $arrayNow = $arrayAll;
        }
        return array_diff($arrayNow, $unArray); // 去除无用的
    }
}

if (!function_exists('make_path')) {
    /**
     * 上传路径转化,默认路径
     * @param $path
     * @param int $type
     * @param bool $force
     * @return string
     * @throws Exception
     */
    function make_path($path, int $type = 2, bool $force = false): string
    {
        $path = DIRECTORY_SEPARATOR . ltrim(rtrim($path));
        switch ($type) {
            case 1:
                $path .= DIRECTORY_SEPARATOR . date('Y');
                break;
            case 2:
                $path .= DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m');
                break;
            case 3:
                $path .= DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
                break;
        }
        try {
            if (is_dir(app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads' . $path, 0777, true) == true) {
                return trim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '.');
            } else return '';
        } catch (Exception $e) {
            if ($force)
                throw new Exception($e->getMessage());
            return '无法创建文件夹，请检查您的上传目录权限：' . app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attach' . DIRECTORY_SEPARATOR;
        }

    }
}
